<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Game library class
 *
 * Game setting information's CRUD.
 *
 */
class Game_lib
{
	/**
	 * Constructor
	 *
	 * Load all games model.
	 */
	public function __construct()
	{
		$this->CI = &get_instance();

		$this->CI->load->model('pf/app/app_info_model');
		$this->CI->load->model('pf/user/user_login_app_model');
		$this->CI->load->model('pf/app/blacklist_model');
		$this->CI->load->model('pf/appupdate/app_ver_update_model');
	}

	/**
	 * Get all games information.
	 *
	 * @param	integer	user id
	 * @param	integer page number
 	 * @param	integer number per page
	 * @return	object	game objects
	 */
	public function get_list($os = 0, $page = 0, $num = 20)
	{
		$games = $this->CI->app_info_model->get_online_apps($os, $page, $num);
		$black_os = get_os($os);
		$black_game_list = $this->CI->blacklist_model->get_all_appids(3,$black_os);
		$list = array();
		foreach ($games as $g)
		{
			if(in_array($g['app_id'],$black_game_list))
				continue;
			
			$game['app_id'] = $g['app_id'];
			$game['name'] = $g['name'];
			//$game['icon'] = $this->CI->config->item('res_domain').$g['icon'];
			$game['icon'] = get_img($g['icon']);
			$game['user_num'] = $g['user_num'];
			$game['download_url'] = 'http://sbpre.log.us.kick9.com/download.apk?appid=3100000001';
				
			array_push($list, $game);
		}

		return $list;
	}
	
	/**
	 * Get game information with game id.
	 *
	 * @param	integer	game id
	 * @return	object	game objects
	 */
	public function get_game($gameid, $chcode)
	{
		//get os int
		$array = $this->CI->uri->uri_to_assoc(4);
		$black_os = get_os($array['os']);
		
		$black_game_list = $this->CI->blacklist_model->get_all_appids(3,$black_os);

		if(in_array($gameid, $black_game_list))
			return false;
		
		$game = $this->CI->app_info_model->get_app($gameid);
		
		unset($game['id']);
		unset($game['size']);
		unset($game['weight']);
		for($i=1; $i<=5; $i++)
			$game['pic'.$i] = $this->CI->config->item('res_domain').$game['pic'.$i];
		//$game['icon'] = $this->CI->config->item('res_domain').$game['icon'];
		$game['icon'] = get_img($game['icon']);
		unset($game['status_android']);
		unset($game['status_ios']);
		unset($game['status_wp']);
		
		$ret = $this->CI->app_ver_update_model->get_by_appid_chcode($gameid, $chcode);
		$game['url'] = empty($ret) ? '' : $ret['uri'];
		$game['download_url'] = 'http://sbpre.log.us.kick9.com/download.apk?appid=3100000001';
		
		return $game;
	}
	
	/**
	 * Get played games regarding user id.
	 *
	 * @param	integer	user id
	 * @return	object	game objects
	 */
	public function get_played_games($uid)
	{
		$gs = $this->CI->user_login_app_model->get_apps($uid);
		
		//get os int
		$array = $this->CI->uri->uri_to_assoc(4);
		$black_os = get_os($array['os']);
		$black_game_list = $this->CI->blacklist_model->get_all_appids(1,$black_os);
		
		$games = array();
		foreach ($gs as $g)
		{
			$game1 = $this->CI->app_info_model->get_app($g['app_id']);
			if(!empty($game1) && !in_array($game1['app_id'],$black_game_list)) {
				$game['app_id'] = $game1['app_id'];
				$game['name'] = $game1['name'];
				//$game['icon'] = $this->CI->config->item('res_domain').$game1['icon'];
				$game['icon'] = get_img($game1['icon']);
				array_push($games, $game);
			}
		}
	
		return $games;
	}
}